package com.czk.yygh.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czk.enums.OrderStatusEnum;
import com.czk.model.order.OrderInfo;
import com.czk.vo.order.OrderCountQueryVo;
import com.czk.vo.order.OrderQueryVo;
import com.czk.yygh.common.result.Result;
import com.czk.yygh.common.utils.AuthContextHolder;
import com.czk.yygh.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/29 15:20
 */
@RestController
@RequestMapping("/api/order/orderInfo")
@Api(tags = "订单管理")
public class OrderApiController {
    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "创建订单")
    @PostMapping("/auth/submitOrder/{scheduleId}/{patientId}")
    public Result saveOrders(@PathVariable String scheduleId,
                             @PathVariable Long patientId) {
        Long orderId = orderService.saveOrder(scheduleId, patientId);
        return Result.ok(orderId);
    }

    /**
     * 订单详情功能
     *
     * @param orderId
     * @return
     */
    @GetMapping("/auth/getOrders/{orderId}")
    public Result getOrders(@PathVariable String orderId) {
        OrderInfo orderInfo = orderService.getOrders(orderId);
        return Result.ok(orderInfo);
    }

    /**
     * 订单的列表的功能
     *
     * @param page
     * @param limit
     * @param orderQueryVo
     * @return
     */
    @GetMapping("/auth/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       OrderQueryVo orderQueryVo,
                       HttpServletRequest request) {
        // 设置当前用户的id值
        Long userId = AuthContextHolder.getUserId(request);
        orderQueryVo.setUserId(userId);
        Page<OrderInfo> pageParams = new Page<>(page, limit);
        IPage<OrderInfo> iPage = orderService.selectPage(pageParams, orderQueryVo);
        return Result.ok(iPage);
    }

    @ApiOperation(value = "获取订单的状态")
    @GetMapping("/auth/getStatusList")
    public Result getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }

    /**
     * 取消预约
     *
     * @param orderId
     * @return
     */
    @GetMapping("/auth/cancelOrder/{orderId}")
    @ApiOperation(value = "取消预约")
    public Result cancelOrder(@PathVariable Long orderId) {
        boolean b = orderService.cancelOrder(orderId);
        return Result.ok(b);
    }

    /**
     * 获取预约统计数据
     *
     * @param orderCountQueryVo
     * @return
     */
    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("/inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getCountMap(orderCountQueryVo);
    }
}
